home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / FontShow-1.1.2-c / Fontshow.c next >
Encoding:
Text File  |  1994-12-04  |  21.3 KB  |  833 lines  |  [TEXT/MMCC]

  1. //•-----------------------------------------------------------------------o//
  2. //•    Note from Kenneth A. Long:
  3. //•     I found this source on the net, but it was only the .c and .h file.
  4. //• Then after a time, I found the built application in a different place
  5. //• and was able to get the resources out of it.  Then I made the .r file
  6. //• from it, for general principles.
  7. //• The two includes I excluded caused link errors I couldn't fix, but it
  8. //• seems to run okay at first glance.  I've done hardly any testing, and
  9. //• I'm sure there could be improvements, refinements and more thorough
  10. //• code added.  But HEY!  It's C source!
  11.  
  12. //•      6. March      1994 (v1.1.2) Compiles/runs - Think C v5.0.4/6.0.1
  13. //•-----------------------------------------------------------------------o//
  14. //o The FontShow
  15.  
  16. //o (c) 1989 Rainer Fuchs, Am Weingarten 13, 6000 Frankfurt 90, FRG
  17.  
  18. //o History:
  19. //•     23. November  1994 (v1.1.2) Runs on Code Warrior C.
  20. //•     06. March      1994 (v1.1.2) Runs on Think C.
  21. //o     17. Oktober      1989 (v1.1.1) Compatible with EZ-Menu 
  22. //o     10. Oktober      1989 (v1.1)    Font number added; Fontsize added
  23. //o      3. Oktober      1989 (v1.0)    multiple sample strings
  24. //o      2. Oktober      1989 (v1.0b3) changed print format 
  25. //o     26. September 1989 (v1.0b2) changed print format
  26. //o     25. September 1989 (v1.0b1) added character table 
  27. //o     24. September 1989 (v0.0d1) first hack
  28. //o-----------------------------------------------------------------------o//
  29.  
  30. #include "fontshow.h"
  31.  
  32. //o-----------------------------------------------------------------------o//
  33. //o Error and information handling ---------------------------------------o//
  34.  
  35. void ErrorMsg (short index)
  36. {
  37.     Str255 theString;
  38.  
  39.     InitCursor ();
  40.     GetIndString (theString, 128, index);//o get error msg from rsrc.
  41.     ParamText (theString, "\p", "\p", "\p");
  42.     StopAlert (128, NIL);            //o show error dialog.
  43. }
  44.  
  45. //o-----------------------------------------------------------------------o//
  46.  
  47. void ShowInfo ()                    //o About FontShow….
  48. {
  49.     DialogPtr     myDialog;
  50.     EventRecord    myEvent;
  51.  
  52.     myDialog=GetNewDialog (ABOUT, NIL, (WindowPtr)-1);//o get dialog from rsrc.
  53.     DrawDialog (myDialog);                    //o draw it.
  54.     while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &myEvent))
  55.         SystemTask ();                //o wait until event.
  56.     DisposDialog (myDialog);        //o dispose of dialog.
  57. }
  58.  
  59. //o-----------------------------------------------------------------------o//
  60. //o Sample text output ---------------------------------------------------o//
  61. //o-----------------------------------------------------------------------o//
  62.  
  63. Boolean BreakCheck ()                //o check for break after each page.
  64. {
  65.     EventRecord theEvent;
  66.     WindowPtr     whichWindow;
  67.  
  68.      for (;;) 
  69.      {    //o mouse clicks in menu bar ignored for comp. with EZ-Menu 
  70.          //o *sigh*.
  71.          while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &theEvent))
  72.             SystemTask ();                //o wait until event.
  73.          if ((theEvent.what == keyDown) || (theEvent.what == autoKey)) //o keyDown?.
  74.             return ((char)BitAnd (theEvent.message, charCodeMask) == '.' &&
  75.                         BitAnd (theEvent.modifiers, cmdKey) != 0);
  76.             //o if Cmd-dot    then TRUE else FALSE.
  77.          else
  78.              if (theEvent.what == mouseDown && FindWindow (theEvent.where, &whichWindow)!=inMenuBar)
  79.                      return (FALSE);//o click not in MenuBar.
  80.      }
  81. }
  82.  
  83. //o-----------------------------------------------------------------------o//
  84.  
  85. void DrawPage (short i)//o draw sample page.
  86. {
  87.     myFontInfo     *myFont;
  88.     short             lineNr=0;
  89.     char             *pos;
  90.     short from, to, nr;
  91.     short c, x, y;
  92.     short            lineH;
  93.     short            wMax;
  94.     Str255        myStr;
  95.     char            buf[4];
  96.  
  97.     myFont= *fontHdl;                //o Block is already locked !.
  98.     lineH=myFont[i].lineHeight;
  99.     wMax=myFont[i].widMax;
  100.  
  101.     TextFont (0);                    //o draw font name in system font.
  102.     TextSize (12);                    //o and 12 pt size.
  103.     MoveTo (TOPH, TOPF);            //o    goto title line.
  104.     DrawString (myFont[i].fontName);//o draw font name.
  105.     DrawString ("\p (ID=");
  106. //    stci_d (buf, myFont[i].fontNum, 3);    //o KAL toox it out - couldn't...
  107.     DrawString (CtoPstr (buf));
  108.     DrawString ("\p) ");
  109. //    stci_d (buf, fontSize, 2);            //o find in headers.
  110.     DrawString (CtoPstr (buf));
  111.     DrawString ("\p point.");
  112.  
  113.     TextFont (myFont[i].fontNum);//o set font.
  114.     TextSize (fontSize);
  115.  
  116.     if (RealFont (myFont[i].fontNum, fontSize)) 
  117.     {
  118.         //o sample text.
  119.         if (opt == SAMPLE || prFlag) 
  120.         {
  121.             lineNr++;
  122.             HLock (myText);
  123.             from=nr=to=0;
  124.             pos=*myText;
  125.             
  126.             //o break text into single lines.
  127.             while (to<textLength) 
  128.             {    
  129.                 to++;
  130.                 
  131.                 //o search for CR.
  132.                 if ((*pos++ == 0x0D) || (to == textLength)) 
  133.                 {
  134.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );//o go to next line.
  135.                     DrawText (*myText, from, to-from);//o draw line.
  136.                     from = to;
  137.                 }
  138.             }
  139.             HUnlock (myText);
  140.         }
  141.  
  142.         //o sample string.
  143.         if (opt == STRING || prFlag ) 
  144.         {
  145.             for (i=1;i;i++) 
  146.             {
  147.                 lineNr++;
  148.                 GetIndString (myStr, STRINGS, i);
  149.                 if (*myStr) 
  150.                 {
  151.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );
  152.                     DrawString (myStr);
  153.                 }
  154.                 else break;
  155.             }
  156.         }
  157.         //o character map.
  158.         if (opt == MAP || prFlag) 
  159.         {
  160.             lineNr++;
  161.             
  162.             //o only printable characters.
  163.             for (c = 32; c < 256; c++) 
  164.             {    
  165.                 //o 32 char per row.
  166.                 if (c % 32 == 0) 
  167.                 {
  168.                     x = TOPH;
  169.                     y = TOPV + lineH*lineNr++;    //o new line.
  170.                 }
  171.                 MoveTo (x, y);
  172.                 if (isprint (c))                //o skip 127.
  173.                     DrawChar ((char)c);
  174.                 else
  175.                     DrawChar (32);
  176.                 x += wMax;                    //o makes a pretty output.
  177.             }
  178.         }
  179.     }
  180.     else 
  181.         {
  182.             TextSize (12);
  183.             TextFont (0);
  184.             DrawString ("\p Font size not available.");
  185.     }
  186. }
  187.  
  188. //o-----------------------------------------------------------------------o//
  189.  
  190. void ShowFonts ()                    //o Display fonts.
  191. {
  192.     short i;
  193.     DialogPtr    myDialog;
  194.     WindowPtr     myWindow;
  195.  
  196.     prFlag = FALSE;
  197.  
  198.     if (SelectFonts ()) 
  199.     {            //o only when select dialog is ok.
  200.         myWindow = GetNewWindow (FONTWINDOW, NIL, (WindowPtr)-1L); //o open a window.
  201.         myDialog = GetNewDialog (SCREENBREAK, NIL, myWindow);//o show break information.
  202.         DrawDialog (myDialog);
  203.         SetPort (myWindow);
  204.         HLock ((Handle) fontHdl);
  205.         for (i = 0; i < fontCount; i++) 
  206.         {
  207.             //o draw only selected fonts.
  208.             if ((*fontHdl)[i].flag)
  209.             {
  210.                 EraseRect (&myWindow->portRect);    //o    clear page.
  211.                 DrawPage (i);                        //o draw sample page.
  212.                 if (BreakCheck ())                    //o check for break.
  213.                     break;
  214.             }
  215.         }
  216.         HUnlock ((Handle) fontHdl);
  217.         DisposeWindow (myWindow);//o close window and dialog.
  218.         DisposDialog (myDialog);
  219.     }
  220. }
  221.  
  222. //o-----------------------------------------------------------------------o//
  223.  
  224. short HowMany ()
  225. {
  226.     return (((**prRecHdl).prJob.bJDocLoop == bDraftLoop) ? 
  227.                 (**prRecHdl).prJob.iCopies : 1 );
  228. }
  229.  
  230. //o-----------------------------------------------------------------------o//
  231.  
  232. void PrintFonts ()                    //o print fonts.
  233. {
  234.     TPPrPort        myPrPort;
  235.     TPrStatus    myPrStatus;
  236.     DialogPtr     mySpoolDlg, myPrintDlg;
  237.     GrafPtr        savePort;
  238.     short i;
  239.     short copies;
  240.     short             curs=0;
  241.  
  242.     prFlag=TRUE;                    //o indicate printing.
  243.  
  244.     GetPort (&savePort);            //o save old grafPort.
  245.     PrOpen ();                        //o open PrintMgr.
  246.     
  247.     //o get print job infos.
  248.     if (SelectFonts () && PrJobDialog (prRecHdl)) 
  249.     { 
  250.         //o just in case last printout was cancelled by user:.
  251.         PrintErr=noErr;
  252.         for (copies = HowMany (); copies > 0 && PrintErr == noErr; copies--) 
  253.         {
  254.             //o show status information:.
  255.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop) 
  256.                 DrawDialog (mySpoolDlg=GetNewDialog (SPOOLINFO, NIL, (WindowPtr)-1));
  257.             else
  258.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  259.  
  260.             SetCursor (*myCursor[curs++]);//o we´re busy (rotating watch.
  261.             if (curs == 4) curs = 0;
  262.  
  263.             myPrPort = PrOpenDoc (prRecHdl, NIL, NIL); //o open printPort.
  264.             SetPort ((GrafPtr) myPrPort);
  265.             TextSize (12);
  266.             HLock ((Handle) fontHdl);
  267.             for (i = 0; i < fontCount; i++) 
  268.             {
  269.  
  270.                 SetCursor (*myCursor[curs++]);//o still busy.
  271.                 if (curs == 4) curs = 0;
  272.  
  273.                 if (PrintErr == noErr) 
  274.                 {
  275.                     //o print only selected fonts.
  276.                     if ((*fontHdl)[i].flag)
  277.                     {
  278.                         PrOpenPage (myPrPort, NIL);
  279.                         if (PrintErr == noErr)
  280.                             DrawPage (i);            //o print sample page.
  281.                         PrClosePage (myPrPort);
  282.                     }
  283.                 }
  284.             }
  285.             HUnlock ((Handle) fontHdl);
  286.             PrCloseDoc (myPrPort);//o close printPort.
  287.  
  288.             //o now print spool file.
  289.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop && PrintErr == noErr) 
  290.             {
  291.                 DisposDialog (mySpoolDlg);
  292.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  293.                 PrPicFile (prRecHdl, NIL, NIL, NIL, &myPrStatus); //o print spool file.
  294.             }
  295.  
  296.             DisposDialog (myPrintDlg);
  297.         }
  298.  
  299.         InitCursor ();
  300.         if (PrintErr!=noErr)
  301.                 ErrorMsg (ERR_PRINT);//o something went wrong.
  302.         else
  303.             SysBeep (10);            //o notify user.
  304.         PrClose ();                    //o close printing manager.
  305.         SetPort (savePort);        //o reset old grafPort.
  306.     }
  307. }
  308.  
  309. //o-----------------------------------------------------------------------o//
  310. //o Font dialog handling -------------------------------------------------o//
  311. //o-----------------------------------------------------------------------o//
  312.  
  313. pascal void    DrawList(WindowPtr theWindow, short itemNo)
  314. {
  315.     Rect         rView;
  316.     RgnHandle     theRgn;
  317.     short         itemType;
  318.     Handle         item;
  319.     Rect         box;
  320.         
  321.     //o get rect of OK button.
  322.     GetDItem (theWindow, OK, &itemType, &item, &box); 
  323.     PenSize (3, 3);
  324.     InsetRect (&box, -4, -4);
  325.     FrameRoundRect (&box, 16, 16);    //o draw bold.
  326.     PenSize (1, 1);
  327.  
  328.     HLock ((Handle) myList);
  329.     rView= (**myList).rView;
  330.     InsetRect (&rView, -1, -1);
  331.     FrameRect (&rView);                //o frame around list.
  332.     theRgn= (*theWindow).visRgn;
  333.     LUpdate (theRgn, myList);        //o update list.
  334.     HUnlock ((Handle) myList);
  335. }
  336.  
  337. //o-----------------------------------------------------------------------o//
  338.  
  339. pascal Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, 
  340.                                 short *itemHit)
  341. {
  342.     char     charCode;
  343.     GrafPtr savePort;
  344.     Cell     theCell;
  345.     Boolean flag;
  346.  
  347.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey)) 
  348.     { 
  349.         charCode= (char)BitAnd (theEvent->message, charCodeMask);
  350.         
  351.         //o OK button is default.
  352.         if ((charCode == 13) || (charCode == 3)) 
  353.         { 
  354.             *itemHit=1;
  355.             return (TRUE);
  356.         }
  357.         else if ((charCode == '.') && (BitAnd (theEvent->modifiers, cmdKey) != 0)) 
  358.         {                                //o Cmd-dot cancels dialog.
  359.             *itemHit=2;
  360.             return (TRUE);
  361.         }
  362.         else return (FALSE);
  363.     }
  364.  
  365.     else 
  366.         {
  367.             if (*itemHit == FLIST) 
  368.             {    //o list handling.
  369.                     GetPort (&savePort);
  370.                     SetPort ((**myList).port);
  371.  
  372.                     //o convert to local coordinates.
  373.                     GlobalToLocal (& (theEvent->where));
  374.  
  375.                     //o simulate cmd key to fool default list
  376.                     //o definition routine !!!.
  377.                     flag = LClick (theEvent->where, 
  378.                                    theEvent->modifiers|cmdKey, 
  379.                                    myList);
  380.                                    
  381.                     SetPort (savePort);
  382.             }
  383.  
  384.             if (flag == TRUE) 
  385.             {        //o double click is equal to ok.
  386.                     *itemHit=1;
  387.                     return (TRUE);
  388.                 }
  389.                 else
  390.                     return (FALSE);
  391.     }
  392. }
  393.  
  394. //o-----------------------------------------------------------------------o//
  395. //o Initialize font list. ------------------------------------------------o//
  396. //o-----------------------------------------------------------------------o//
  397.  
  398. void InitList (WindowPtr theWindow)
  399. {
  400.     short             itemType;
  401.     Handle         item;
  402.     Point         csize, theCell;
  403.     Rect             rView, dataBounds;
  404.     short i;
  405.     myFontInfo     *myFont;
  406.  
  407.     GetDItem (theWindow, FLIST, &itemType, &item, &rView);
  408.     SetRect (&dataBounds, 0, 0, 1, fontCount);//o set borders of list.
  409.     SetPt (&csize, 0, 0);
  410.     rView.right-=15;                //o leave room for scroll bar.
  411.     myList=LNew (&rView, &dataBounds, csize, 0, theWindow, TRUE, FALSE, FALSE, TRUE);
  412.  
  413.     HLock ((Handle) fontHdl);
  414.     myFont= *fontHdl;
  415.  
  416.     //o fill list with entries from myFontInfo array.
  417.     for (i=0; i<fontCount;i++) 
  418.     {
  419.         SetPt (&theCell, 0, i);
  420.         LSetCell (myFont[i].fontName+1, *myFont[i].fontName, theCell, myList);
  421.         LSetSelect (myFont[i].flag, theCell, myList);//o preselect entries.
  422.     }
  423.     HUnlock ((Handle) fontHdl);
  424. }
  425.  
  426. //o-----------------------------------------------------------------------o//
  427.  
  428. Boolean SelectFonts ()            //o handle font select dialog.
  429.     short         i;
  430.     short        button;
  431.     short         itemType;
  432.     Handle         item;
  433.     Rect         box;
  434.     Cell         theCell;
  435.     Boolean     stop=FALSE;
  436.     myFontInfo     *myFont;
  437.     DialogPtr     myDialog = GetNewDialog (FONTLIST, NIL, (WindowPtr)-1);
  438.                                             //o Auswahl-Dialog.
  439.     
  440.     GetDItem (myDialog, FLIST, &itemType, &item, &box);//o get rect of list button.
  441.     SetDItem (myDialog, FLIST, userItem, *DrawList, &box); //o install user item.
  442.  
  443.     InitList (myDialog);             //o initialize list.
  444.  
  445.     ShowWindow (myDialog);        //o make dialog visible.
  446.     while (!stop) 
  447.     {
  448.         SystemTask ();
  449.         ModalDialog (myFilter, &button);//o Dialog anzeigen.
  450.  
  451.         switch (button) 
  452.         {
  453.             case EXALL:                //o deselect all fonts.
  454.                 for (i=0; i<fontCount;i++) 
  455.                 {
  456.                         SetPt (&theCell, 0, i);
  457.                         LSetSelect (FALSE, theCell, myList);
  458.                 }
  459.             break;
  460.  
  461.             case INALL:                //o select all fonts.
  462.                 for (i=0; i<fontCount;i++) 
  463.                 {
  464.                         SetPt (&theCell, 0, i);
  465.                         LSetSelect (TRUE, theCell, myList);
  466.                 }
  467.             break;
  468.  
  469.             case REVERT:            //o revert to old. 
  470.                 HLock ((Handle) fontHdl);
  471.                 myFont= *fontHdl;
  472.                 for (i=0; i<fontCount;i++) 
  473.                 {
  474.                         SetPt (&theCell, 0, i);
  475.                         LSetSelect (myFont[i].flag, theCell, myList);
  476.                 }
  477.                 HUnlock ((Handle) fontHdl);
  478.             break;
  479.  
  480.             case Cancel:            //o dialog cancelled.
  481.                 stop=TRUE;
  482.             break;
  483.  
  484.             case OK:
  485.                  HLock ((Handle) fontHdl);
  486.                 myFont= *fontHdl;
  487.                 for (i=0; i<fontCount;i++) 
  488.                 {//o update myFontInfo array.
  489.                         SetPt (&theCell, 0, i);
  490.                         myFont[i].flag=LGetSelect (FALSE, &theCell, myList);
  491.                 }
  492.                 HUnlock ((Handle) fontHdl);
  493.                 stop=TRUE;
  494.                 break;
  495.         } //o end case.
  496.     }
  497.  
  498.     LDispose (myList);                //o dispose of list.
  499.     DisposDialog (myDialog);                    //o dispose of dialog.
  500.     return ((button == OK)?TRUE:FALSE);
  501. }
  502.  
  503. //o-----------------------------------------------------------------------o//
  504. //o Event handling -------------------------------------------------------o//
  505. //o-----------------------------------------------------------------------o//
  506.  
  507. void HandleMenu (sel)                //o handle menu bar.
  508. long sel;
  509. {
  510.     short     theItem=LoWord (sel);
  511.     Str255 name;
  512.     GrafPtr savePort;
  513.     short i;
  514.     myFontInfo *myFont;
  515.     FontInfo    fInfo;
  516.  
  517.     switch (HiWord (sel)) 
  518.     {
  519.         case APPLE_M:
  520.             GetItem (myMenus[APPLE], theItem, (StringPtr) &name);
  521.             if (theItem == INFO)        //o About….
  522.                 ShowInfo ();
  523.             else 
  524.                 {
  525.                     GetPort (&savePort);//o save old grafPort (just in case…).
  526.                     OpenDeskAcc ((StringPtr) &name);//o open DA.
  527.                     InitCursor ();        //o DA may have changed cursor.
  528.                     SetPort (savePort);    //o    DA may have changed grafPort.
  529.             }
  530.         break;
  531.  
  532.         case FILE_M:
  533.             switch (theItem) 
  534.             {
  535.                 case SETUP:
  536.                     PrOpen ();        //o open/close PrintMgr acc. to TN.
  537.                     PrStlDialog (prRecHdl);//o get page setup.
  538.                     PrClose ();
  539.                 break;
  540.  
  541.                 case DISPLAY:
  542.                     ShowFonts ();
  543.                 break;
  544.  
  545.                 case PRINT:
  546.                     PrintFonts ();
  547.                 break;
  548.  
  549.                 case QUIT:
  550.                     quit=TRUE;
  551.                 break;
  552.             }
  553.         break;
  554.  
  555.         case EDIT_M: 
  556.             SystemEdit (theItem-1); //o our application doesn´t handle EDIT menu.
  557.         break;
  558.  
  559.         case OPTIONS_M:
  560.             CheckItem (myMenus[OPTIONS], opt, FALSE); //o deselect menu item.
  561.             CheckItem (myMenus[OPTIONS], opt=theItem, TRUE); //o select new Item.
  562.         break;
  563.  
  564.         case SIZE_M:
  565.             if (theItem!=oldItem) 
  566.             {
  567.                 SetCursor (*myCursor[0]);
  568.                 CheckItem (myMenus[SIZE], oldItem, FALSE);
  569.                 CheckItem (myMenus[SIZE], oldItem=theItem, TRUE);
  570.                 switch (oldItem) 
  571.                 {
  572.                     case SIZE9:
  573.                         fontSize=9;
  574.                     break;
  575.  
  576.                     case SIZE10:
  577.                         fontSize=10;
  578.                     break;
  579.  
  580.                     case SIZE12:
  581.                         fontSize=12;
  582.                     break;
  583.  
  584.                     case SIZE14:
  585.                         fontSize=14;
  586.                     break;
  587.  
  588.                     case SIZE18:
  589.                         fontSize=18;
  590.                     break;
  591.  
  592.                     case SIZE20:
  593.                         fontSize=20;
  594.                     break;
  595.  
  596.                     case SIZE24:
  597.                         fontSize=24;
  598.                     break;
  599.                 }
  600.                 TextSize (fontSize);
  601.                 HLock ((Handle) fontHdl);
  602.                 myFont= *fontHdl;
  603.                 for (i = 0; i < fontCount; i++) 
  604.                 {
  605.                 TextFont (myFont[i].fontNum);                //o set font.
  606.                 GetFontInfo (&fInfo);                            //o and get font info.
  607.                 myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  608.                 myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  609.             }
  610.             HUnlock ((Handle) fontHdl);
  611.             TextSize (12);
  612.             TextFont (0);
  613.             InitCursor ();
  614.         }
  615.         break;
  616.     }
  617.     HiliteMenu (0);
  618. }
  619.  
  620. //o-----------------------------------------------------------------------o//
  621.  
  622. void HandleMouseDown (theEvent)//o handle mouseDown events.
  623. EventRecord theEvent;
  624. {
  625.     WindowPtr whichWindow;
  626.  
  627.     switch (FindWindow (theEvent.where, &whichWindow )) 
  628.     {
  629.         case inDesk:
  630.             SysBeep (10);
  631.         break;
  632.  
  633.         case inMenuBar:
  634.             HandleMenu (MenuSelect (theEvent.where) ) ;
  635.         break;
  636.  
  637.         case inSysWindow:
  638.             SystemClick (&theEvent, whichWindow );
  639.         break;
  640.     } //o end switch.
  641. }
  642.  
  643. //o-----------------------------------------------------------------------o//
  644. //o MainLoop () -----------------------------------------------------------o//
  645. //o-----------------------------------------------------------------------o//
  646.  
  647. void MainLoop ()
  648. {
  649.     EventRecord theEvent;
  650.  
  651.     quit = FALSE;
  652.     while (!quit)
  653.     {
  654.         SystemTask ();
  655.         if (GetNextEvent (everyEvent, &theEvent))
  656.             switch (theEvent.what) 
  657.             {
  658.                 case mouseDown:    //o mouse click.
  659.                     HandleMouseDown (theEvent);
  660.                 break;
  661.  
  662.                 case keyDown:         //o key click.
  663.                 case autoKey:
  664.                     if ((theEvent.modifiers & cmdKey) != 0) //o we handle only Cmd-key.
  665.                         HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  666.                 break;
  667.             } //o end switch (and if).
  668.     }//o end while.
  669. }
  670.  
  671. //o-----------------------------------------------------------------------o//
  672. //o Initialization and main () --------------------------------------------o//
  673. //o-----------------------------------------------------------------------o//
  674.  
  675. void InitMenu ()                    //o Draw menu bar.
  676. {
  677.     short i;
  678.  
  679.     myMenus[APPLE]=GetMenu (APPLE_M);
  680.     AddResMenu (myMenus[APPLE], 'DRVR');//o insert DAs in Apple-menu.
  681.  
  682.     myMenus[FILE]=GetMenu (FILE_M);
  683.     myMenus[EDIT]=GetMenu (EDIT_M);
  684.     myMenus[OPTIONS]=GetMenu (OPTIONS_M);
  685.     myMenus[SIZE]=GetMenu (SIZE_M);
  686.  
  687.     CheckItem (myMenus[OPTIONS], opt = SAMPLE, TRUE); //o preselect "sample text".
  688.     CheckItem (myMenus[SIZE], oldItem=SIZE12, TRUE); //o preselect font size 12.
  689.     fontSize=12;
  690.  
  691.     for (i = 0; i < 5; InsertMenu (myMenus[i++], 0))
  692.         ;
  693.     DrawMenuBar ();
  694. }
  695.  
  696. //o-----------------------------------------------------------------------o//
  697.  
  698. void InitText ()                    //o get sample text from rsrc.
  699. {
  700.     myText = GetResource ('TEXT', 128);
  701.     textLength= (short)SizeResource (myText);
  702. }
  703.  
  704. //o-----------------------------------------------------------------------o//
  705.  
  706. void InitPrint ()
  707. {
  708.     PrOpen ();
  709.     prRecHdl= (THPrint)NewHandle (sizeof (TPrint));//o get new print record.
  710.     if (prRecHdl)
  711.         PrintDefault (prRecHdl);    //o validate it.
  712.     else
  713.         ErrorMsg (ERR_NOROOM);
  714.     PrClose ();
  715. }
  716.  
  717. //o-----------------------------------------------------------------------o//
  718.  
  719. void BuildList ()                    //o build font list.
  720. {
  721.     register     short i;
  722.     myFontInfo    *myFont;
  723.     FontInfo     fInfo;
  724.     MenuHandle    tempMenu;
  725.  
  726.     //o Get a fake menu; use the Menu Manager to fill it with font names - 
  727.     //o the Menu Manager sorts entries alphabetically !.
  728.  
  729.     tempMenu = NewMenu (TEMP_M, "\px");
  730.     AddResMenu (tempMenu, 'FONT');
  731.     fontCount=CountMItems (tempMenu);
  732.  
  733.     //o dynamic allocation of heap space for myFontInfo array.
  734.     if (fontHdl= (myFontInfo **)NewHandle (fontCount*sizeof (myFontInfo))) 
  735.     {
  736.         HLock ((Handle) fontHdl);
  737.         TextSize (12);
  738.         myFont= *fontHdl;
  739.         for (i=0;i<fontCount;i++) 
  740.         {
  741.                 GetItem (tempMenu, i+1, myFont[i].fontName);//o get font names.
  742.             GetFNum (myFont[i].fontName, &myFont[i].fontNum);//o get font numbers.
  743.             TextFont (myFont[i].fontNum);                //o set font.
  744.             GetFontInfo (&fInfo);                            //o and get font info.
  745.             myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  746.             myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  747.             myFont[i].flag=TRUE;                            //o preselect font.
  748.         }
  749.         HUnlock ((Handle) fontHdl);
  750.     }
  751.     else
  752.         ErrorMsg (ERR_NOROOM);
  753.  
  754.     DisposeMenu (tempMenu);//o We’re done with the menu; dispose of it.
  755.     TextFont (0);    //o and reset system font.
  756. }
  757.  
  758. //o-----------------------------------------------------------------------o//
  759.  
  760. void InitThings ()
  761. {
  762.     DialogPtr myDialog;
  763.     short i;
  764.  
  765.     InitGraf (&qd.thePort); 
  766.     MaxApplZone ();                        //o we want it all, and we want it NOW !.
  767.     MoreMasters ();                    //o extra pointer blocks at the bottom of the heap.
  768.     MoreMasters ();                    //o this is 5 X 64 master pointers.
  769.     MoreMasters ();
  770.     MoreMasters ();
  771.     MoreMasters ();
  772.  
  773.     InitFonts ();            //o startup the font manager.
  774.     InitWindows ();                        //o startup the window manager.
  775.     InitMenus ();                    //o startup the menu manager.
  776.     TEInit ();                        //o startup the text edit manager.
  777.     InitDialogs (0);                //o startup the dialog manager.
  778.  
  779.     FlushEvents (everyEvent, 0);
  780.  
  781. //o get the cursors we use and lock them down - no clutter.
  782.  
  783.     myCursor[0] = GetCursor (watchCursor);
  784.     myCursor[1] = GetCursor (128);
  785.     myCursor[2] = GetCursor (129);
  786.     myCursor[3] = GetCursor (130);
  787.  
  788.     for (i = 0; i < 4; i++) 
  789.     {
  790.         MoveHHi ((Handle)myCursor[i]);
  791.         HLock ((Handle)myCursor[i]);
  792.     }
  793.  
  794.     //o tell user we´re busy.
  795.     myDialog = GetNewDialog (WAIT, NIL, (WindowPtr)-1); 
  796.     DrawDialog (myDialog);
  797.     InitMenu ();                        //o build menus.
  798.     InitText ();                        //o get sample text.
  799.     InitPrint ();                    //o initialize printing.
  800.     BuildList ();                    //o build font name list.
  801.     DisposDialog (myDialog);
  802.     InitCursor ();                    //o show arrow cursor.
  803. }
  804.  
  805. //o-----------------------------------------------------------------------o//
  806.  
  807. void CleanUp ()
  808. {
  809.     short i;
  810.  
  811.     if (fontHdl)
  812.         DisposHandle ((Handle) fontHdl);
  813.     if (prRecHdl)
  814.         DisposHandle ((Handle) prRecHdl);
  815.     for (i=0;i<4;i++)
  816.         HUnlock ((Handle)myCursor[i]);
  817. }
  818.  
  819. //o-----------------------------------------------------------------------o//
  820.  
  821. void main ()
  822. {
  823.     InitThings ();                            //o initializations.
  824.     if (fontHdl && prRecHdl)
  825.         MainLoop ();                            //o main routine.
  826.     CleanUp ();                                //o clean up.
  827. }
  828.  
  829. //o-----------------------------------------------------------------------o//
  830. //o Show's over, folks!
  831. //o-----------------------------------------------------------------------o//
  832.